package com.robot.orders.service;

import com.robot.orders.dao.SettlementDetailsDao;
import com.robot.orders.pojo.SettlementDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * settlementDetails服务层
 * 
 * @author wuyunbin
 *
 */
@Service
public class SettlementDetailsService {
	@Autowired
	private SettlementDetailsDao settlementDetailsDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<SettlementDetails> findAll() {
		return settlementDetailsDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<SettlementDetails> findSearch(Map whereMap, int page, int size) {
		Specification<SettlementDetails> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return settlementDetailsDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<SettlementDetails> findSearch(Map whereMap) {
		Specification<SettlementDetails> specification = createSpecification(whereMap);
		return settlementDetailsDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public SettlementDetails findById(String id) {
		return settlementDetailsDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param settlementDetails
	 */
	public void add(SettlementDetails settlementDetails) {
		//雪花分布式ID生成器
		settlementDetails.setId( idWorker.nextId()+"" ); 
		settlementDetailsDao.save(settlementDetails);
	}

	/**
	 * 修改
	 * @param settlementDetails
	 */
	public void update(SettlementDetails settlementDetails) {
		settlementDetailsDao.save(settlementDetails);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		settlementDetailsDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<SettlementDetails> createSpecification(Map searchMap) {
		return new Specification<SettlementDetails>() {
			@Override
			public Predicate toPredicate(Root<SettlementDetails> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 结算明细id
                String id="id";
                if (searchMap.get(id)!=null && !"".equals(searchMap.get(id))) {
                	predicateList.add(cb.like(root.get(id).as(String.class), "%"+(String)searchMap.get(id)+"%"));
                }
                // 代理商id
                String agentId="agentId";
                if (searchMap.get(agentId)!=null && !"".equals(searchMap.get(agentId))) {
                	predicateList.add(cb.like(root.get(agentId).as(String.class), "%"+(String)searchMap.get(agentId)+"%"));
                }
                // 会员id
                String memberId="memberId";
                if (searchMap.get(memberId)!=null && !"".equals(searchMap.get(memberId))) {
                	predicateList.add(cb.like(root.get(memberId).as(String.class), "%"+(String)searchMap.get(memberId)+"%"));
                }
                // 商家id
                String merchantId="merchantId";
                if (searchMap.get(merchantId)!=null && !"".equals(searchMap.get(merchantId))) {
                	predicateList.add(cb.like(root.get(merchantId).as(String.class), "%"+(String)searchMap.get(merchantId)+"%"));
                }
                // 店铺id
                String storeId="storeId";
                if (searchMap.get(storeId)!=null && !"".equals(searchMap.get(storeId))) {
                	predicateList.add(cb.like(root.get(storeId).as(String.class), "%"+(String)searchMap.get(storeId)+"%"));
                }
                // 会员等级
                String memberGrade="memberGrade";
                if (searchMap.get(memberGrade)!=null && !"".equals(searchMap.get(memberGrade))) {
                	predicateList.add(cb.like(root.get(memberGrade).as(String.class), "%"+(String)searchMap.get(memberGrade)+"%"));
                }
                // 0-未提现 1-已提现
                String status="status";
                if (searchMap.get(status)!=null && !"".equals(searchMap.get(status))) {
                	predicateList.add(cb.like(root.get(status).as(String.class), "%"+(String)searchMap.get(status)+"%"));
                }
				//剔除软删除数据
				String deletedAt = "deletedAt";
				predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(Date.class)));

				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
			}
		};
	}

}
